<script setup lang="ts">
import { onMounted, onUpdated, reactive, ref, getCurrentInstance } from 'vue'
import { addBuyer } from '../../api/user'
import { ElMessage, FormInstance } from 'element-plus'
import ball from '../../utils/effect/ball'
import { useStore } from 'vuex'

const store = useStore()

let isShowLogin = ref(false)
const canvas = ref<HTMLCanvasElement>()
let ctx: CanvasRenderingContext2D
onMounted(function () {
  ball(canvas, ctx)
  isShowLogin.value = true
})


const trigger = ref<HTMLElement>()
const handleTrigger = () => {
  trigger.value?.classList.add('active')
  isRegister.value = true
}

let isRegister = ref<boolean>(false)


// 表单区域
const ruleFormRef = ref<FormInstance>()

const loginForm = reactive({
  phone: '',
  password: '',
})

const handleLogin = () => {
  ruleFormRef.value?.validate(valid => {
    if (valid) {
      store.dispatch('user/login', loginForm)
    }
  })
}

const handleRegister = () => {
  addBuyer(loginForm).then((res: any) => {
    if (res.code == 200) {
      ElMessage.success("注册成功");
    } else if (res.code == 500) {
      ElMessage.error(res.msg);
    }else{
      ElMessage.error("注册失败");
    }
  })
}

const rules = reactive({
  phone: [
    { required: true, message: '手机号不能为空', trigger: 'blur' },
    { pattern: /^1((3[0-9])|(4[1579])|(5[0-9])|(6[6])|(7[0-9])|(8[0-9])|(9[0-9]))\d{8}$/, message: '手机格式不正确', trigger: 'blur' },
    { min: 11, max: 11, message: '手机号必须为11位', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 8, max: 20, message: '密码必须为8到20位', trigger: 'blur' }
  ]
})

</script>

<template>
  <div class="login-page center">
    <canvas ref="canvas" class="my-ball">
      Canvas not supported
    </canvas>
    <div class="login-box center">
      <div class="login-form">
        <Transition name="fade">
          <div class="form-title center delay1" v-if="isShowLogin">welcome</div>
        </Transition>
        <Transition name="fade">
          <el-form ref="ruleFormRef" :model="loginForm" :rules="rules" class="trans-center delay2" label-width="auto"
            v-if="isShowLogin">
            <el-form-item prop="phone">
              <el-input v-model="loginForm.phone" autocomplete="true" placeholder="请输入手机号"
                οninput="value=value.replace(/[^\d]/g,'');if(value.length > 11)value = value.slice(0, 11)" />
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="loginForm.password" type="password" autocomplete="true" style="--el-input-height:40px"
                placeholder="请输入密码" />
            </el-form-item>
          </el-form>
        </Transition>
        <Transition name="fade">
          <div class="form-btn-box center delay3" v-if="isShowLogin">
            <el-button type="primary" @click="handleLogin">登录</el-button>
            <el-button type="primary" @click="handleRegister">注册</el-button>
          </div>
        </Transition>
      </div>

      <div class="circle center" @click="handleTrigger" ref="trigger">
        <el-icon :size="50" v-show="!isRegister">
          <Paperclip color='white' />
        </el-icon>
      </div>
    </div>
  </div>

</template>

<style scoped lang="scss">
::v-deep .el-form-item__label {
  height: 40px;
  line-height: 40px;
}

::v-deep .el-button {
  width: 30%;
  height: 40px;
}

::v-deep .el-input {
  --el-input-height: 40px;
  --el-input-bg-color: transparent;
  --el-input-force-border-color: transparent;
  --el-input-text-color: white --el-input-placeholder-color: white;
  --el-input-placeholder-color: white;
  --el-input-hover-border-color: transparent;
  --el-input-clear-hover-color: transparent;
  --el-input-focus-border-color: transparent;
  --el-input-border-color: transparent;

  border: none;
  border-bottom: 2px solid pink;
  color: white
}

.my-ball {
  width: 100%;
  height: 100%;
  position: absolute;
  z-index: -10;
}

.login-form {
  position: relative;
  width: 60%;
  height: 80%;


  .form-title {
    position: relative;
    top: 60px;
    -webkit-text-stroke: 1px orange;
    -webkit-text-fill-color: transparent;
    font-size: 50px;
    color: #fff;
  }


  .form-btn-box {
    position: absolute;
    bottom: 20px;
    width: 100%;
  }

}

.login-page {
  height: 100%;
  width: 100%;
  overflow: hidden;

}

.login-box {
  position: relative;
  width: 800px;
  height: 500px;
  border-radius: 20px;
  box-shadow: 5px 5px 20px 10px #fff;
  backdrop-filter: blur(5px);
}

.circle {
  background-color: pink;
  border-radius: 50%;
  position: absolute;
  width: 100px;
  height: 100px;
  left: 750px;
  top: 100px;
}

.active {
  animation: change 1s linear 1;
  transition: all 1s;
  animation-fill-mode: forwards;
}

@keyframes change {
  10% {
    left: 150px;
    top: 300px;
    width: 50px;
    height: 50px;
  }

  20% {
    left: 0px;
    top: 0px;
    height: 500px;
    width: 800px;
    border-radius: 20px;
  }

  100% {
    left: 0px;
    top: 0px;
    height: 500px;
    width: 800px;
    border-radius: 20px;
  }
}
</style>
